package io.bluefw.blueui.dsl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;

import org.apache.commons.lang3.reflect.FieldUtils;

import io.bluefw.microapp.core.Globalize;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Entity {
	public Class<?> entityClass;
	public Entity(String entity) throws ClassNotFoundException {
		entityClass = Class.forName(entity);
	}
	
	public Globalize getGlobalize(String name) {
		return FieldUtils.getField(entityClass, name, true).getAnnotation(Globalize.class);
	}
	
	public Column getColumn(String name) {
		return FieldUtils.getField(entityClass, name, true).getAnnotation(Column.class);
	}
	
	public Class<?> getFieldType(String name) {
		return FieldUtils.getField(entityClass, name, true).getType();
	}

	public List<EnumItem> getEnumItems(String name) {
		Class<?> enumType = FieldUtils.getField(entityClass, name, true).getType();
		List<EnumItem> items = new ArrayList<>();
		for(Object obj: enumType.getEnumConstants()) {
			Globalize globalize = FieldUtils.getField(enumType, obj.toString()).getAnnotation(Globalize.class);
			items.add(new EnumItem(obj.toString(), globalize.value()));
	    }
		
		return items;
	}
}
